const mysql = require("mysql")
const { MYSQL_CONF } = require("../config/db")

// create connect object
const coon = mysql.createConnection(MYSQL_CONF)

// start connect
coon.connect()

// create unified sql function, function name: exec
function exec(sql) {
  return new Promise((resolve, reject) => {
    coon.query(sql, (error, result) => {
      if (error) {
        reject(error)
        return
      }
      resolve(result)
    })
  })
}

// don't need to close connect
// coon.end()

module.exports = {
  exec,
  escape: mysql.escape
}